package com.jtbd.controller;

import com.jtbd.constants.ResponseCodeEnum;
import com.jtbd.dto.db.UserEmployeeInfo;
import com.jtbd.dto.http.UserEmployeeHttpDto;
import com.jtbd.dto.http.UserStudentHttpDto;
import com.jtbd.dto.result.ResponseBaseData;
import com.jtbd.service.EmployeeBaseOperateService;
import com.jtbd.service.StudentBaseOperateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Tag(name = "用户接口")
@RequestMapping("/login")
public class UserLoginController {
    @Resource
    private StudentBaseOperateService studentOperateService;
    @Resource
    private EmployeeBaseOperateService employeeOperateService;

    @GetMapping("/stuLogin")
    @Operation(summary = "学生用户登录")
    public ResponseBaseData<UserStudentHttpDto> studentLogin(HttpServletResponse response,
                                                             @Parameter(description = "手机号")
                                                             @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式错误")
                                                             @NotBlank(message = "手机号不能为空")
                                                             String phone,
                                                             @Parameter(description = "明文密码  后端做签名比对密文")
                                                             @NotBlank(message = "密码不能为空")
                                                             String password) {
        UserStudentHttpDto userStudentHttpDto = studentOperateService.studentLogin(response, phone, password);
        return ResponseBaseData.success(userStudentHttpDto);
    }

    @GetMapping("/employeeLogin")
    @Operation(summary = "员工登录")
    public ResponseBaseData<List<UserEmployeeInfo>> employeeLogin(HttpServletResponse response,
                                                                     @Parameter(description = "手机号")
                                                                     @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式错误")
                                                                     @NotBlank(message = "手机号不能为空")
                                                                     String phone,
                                                                     @Parameter(description = "明文密码  后端做签名比对密文")
                                                                     @NotBlank(message = "密码不能为空")
                                                                     String password) {
        List<UserEmployeeInfo> userEmployeeHttpDtos = employeeOperateService.employeeLogin(response, phone, password);
        return ResponseBaseData.success(userEmployeeHttpDtos);
    }

    @GetMapping("/employeeConfimLogin")
    @Operation(summary = "员工确认登录身份")
    public ResponseBaseData<UserEmployeeInfo> employeeConfimLogin(HttpServletResponse response, @Parameter(description = "员工id") Long eid) {
        UserEmployeeInfo userEmployeeHttpDto = employeeOperateService.employeeConfimLogin(response, eid);
        if (ObjectUtils.isEmpty(userEmployeeHttpDto)) {
            return ResponseBaseData.error(ResponseCodeEnum.NOT_EXIST);
        }
        return ResponseBaseData.success(userEmployeeHttpDto);
    }
}
